package adv6;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author oldliu
 * @since 1.0
 */
public class ClsTest2 {
    public static void main(String[] args) throws Exception {
        Class c=SuperMarket.class;
        Constructor cr = c.getDeclaredConstructor(String.class,String.class);
        cr.setAccessible(true);//解除private限制
        //相当于调用构造方法
        SuperMarket sp=(SuperMarket) cr.newInstance("家乐福","马鞍山路19号");
        sp.show();
        Field name=c.getDeclaredField("name");
        name.setAccessible(true);
        Object o = name.get(sp);
        System.out.println("name="+o);
        name.set(sp,"红府超市");
        sp.show();
        //sp.setAddr("东明路123号");
        Method m=c.getDeclaredMethod("setAddr", String.class);
        m.invoke(sp,"东明路123号");
        sp.show();
    }
}
